package com.lyt.reggie.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyt.reggie.common.CustomException;
import com.lyt.reggie.entity.Category;
import com.lyt.reggie.entity.Dish;
import com.lyt.reggie.entity.Setmeal;
import com.lyt.reggie.mapper.CategoryMapper;
import com.lyt.reggie.service.CategoryService;
import com.lyt.reggie.service.DishService;
import com.lyt.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Author:李雨涛
 * Date:2023-02-26-14:06
 * 描述：
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Override
    public void remove(Long id) {
        //查询是否关联了菜品(根据Dish)
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1= dishService.count(dishLambdaQueryWrapper);
        if (count1>0){
            throw new CustomException("当前分类下关联了菜品，不能删除");

        }

        //查询是否关联了套餐（根据Setmeal）
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2=setmealService.count(setmealLambdaQueryWrapper);
        if (count2>0){
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }

        //若都没有，贼删除成功
        super.removeById(id);

    }
}
